﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using GSBA.ViewModels;

namespace GSBA.Controls
{
    /// <summary>
    /// Custom gridview allowing different-sized tiles layout for the items in the grid
    /// Based on sample implementation by markri
    /// </summary>
    public class CustomGridView : GridView
    {
        private int rowVal;
        private int colVal;

        /// <summary>
        /// Odd sequence consists of 2 column arrangement, first column with 60/40 row split, second column is 2 row span
        /// </summary>
        private IList<Size> oddSequence;

        /// <summary>
        /// Even sequence consists of 2 row arrangement, first row with 2 column span, second row with 50/50 column split
        /// </summary>
        private IList<Size> evenSequence;

        public CustomGridView()
        {
            this.oddSequence = new List<Size> 
            { 
                LayoutSizesOdd.PrimaryItem, 
                LayoutSizesOdd.SecondaryItem,
                LayoutSizesOdd.TerneryItem
            };

            this.evenSequence = new List<Size>
            {
                LayoutSizesEven.PrimaryItem, 
                LayoutSizesEven.SecondaryItem,
                LayoutSizesEven.TerneryItem
            };
        }

        /// <summary>
        /// Specify how to layout the navigation items. Used to provide some variety to the tile sizes and layout configuration
        /// If we are an odd numbered category, use the oddSequence layout configuration
        /// If we are an even numbered category, use the evenSequence layout configuration
        /// </summary>
        /// <param name="element">UI object</param>
        /// <param name="item">Bound object</param>
        protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
            NavigationItem dataItem = item as NavigationItem;
            int index = -1;

            if (dataItem != null)
            {
                index = dataItem.Category.Items.IndexOf(dataItem);
            }

            var sequence = dataItem.Category.CategoryIndex % 2 == 0 ? evenSequence : oddSequence;

            if (index >= 0 && index < sequence.Count)
            {
                colVal = (int)sequence[index].Width;
                rowVal = (int)sequence[index].Height;
            }
            else
            {
                colVal = (int)LayoutSizesOdd.TerneryItem.Width;
                rowVal = (int)LayoutSizesOdd.TerneryItem.Height;
            }

            VariableSizedWrapGrid.SetRowSpan(element as UIElement, rowVal);
            VariableSizedWrapGrid.SetColumnSpan(element as UIElement, colVal);
        }
    }

    /// <summary>
    /// The layout sizes for the tiles in an odd configuration
    /// </summary>
    static class LayoutSizesOdd
    {
        public static Size PrimaryItem = new Size(6, 2);

        public static Size SecondaryItem = new Size(6, 1);

        public static Size TerneryItem = new Size(3, 3);
    }

    /// <summary>
    /// The layout sizes for the tiles in an even configuration
    /// </summary>
    static class LayoutSizesEven
    {
        public static Size PrimaryItem = new Size(8, 2);

        public static Size SecondaryItem = new Size(4, 1);

        public static Size TerneryItem = new Size(4, 1);
    }
}
